package com.wd.health.model.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.facebook.drawee.view.SimpleDraweeView;

import com.wd.health.R;
import com.wd.health.model.bean.MyMessageBean;
import com.wd.health.model.bean.TalkBean;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
    2020年12月17日18:32:28
    功能:聊天页面适配器
 */
public class AdvisoryPageMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  private List<MyMessageBean> list;
  private Context context;
    private boolean Isstart;
    private String stime = "";
    private AnimationDrawable drawable;

    public AdvisoryPageMessageAdapter(List<MyMessageBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType){
            case 1:
                ViewHodelClient viewHodelClient = new ViewHodelClient(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_hodel_client_entry, parent, false));
                return viewHodelClient;
            case 2:
                ViewHodelPatientEnd viewHodelPatientEnd = new ViewHodelPatientEnd(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_hodel_patients_entry, parent, false));
                return  viewHodelPatientEnd;
            case 3:
                ViewHolderBitMap viewHolderBitMap = new ViewHolderBitMap(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bitmap_ayout, parent, false));
                return  viewHolderBitMap;
            case 4:
                ViewHolderYuYin viewHolderYuYin = new ViewHolderYuYin(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout_yuyin, parent, false));
               return viewHolderYuYin;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        SharedPreferences user = holder.itemView.getContext().getSharedPreferences("user", Context.MODE_PRIVATE);
        String headPic = user.getString("headPic", "");
        //客户端
        if(holder instanceof ViewHodelClient){
            //消息内容赋值
            ((ViewHodelClient) holder).client_context.setText(list.get(position).getContext());
            //头像赋值
            ((ViewHodelClient) holder).client_simpl.setImageURI(headPic);
        }else if(holder instanceof ViewHodelPatientEnd){
        //医生端
            //消息内容赋值
            ((ViewHodelPatientEnd) holder).patients_context.setText(list.get(position).getContext());
            //消息头像赋值
            ((ViewHodelPatientEnd) holder).patients_simpl.setImageResource(R.mipmap.doctor_avatar);
        }else if (holder instanceof ViewHolderBitMap){
            //消息内容赋值
            ((ViewHolderBitMap) holder).img_0001.setImageBitmap(list.get(position).getBitmap());
            //消息头像赋值
            ((ViewHolderBitMap) holder).sdv_user0001.setImageURI(headPic);
        }else if (holder instanceof ViewHolderYuYin){
            //消息头像赋值
            ((ViewHolderYuYin) holder).sdv_user0002.setImageURI(headPic);
            int[] time = list.get(position).getTime();
            int i = time[0];
            stime = String.valueOf(i);
            char c = stime.charAt(0);
            ((ViewHolderYuYin) holder).tv_time.setText(c+"s");
            MediaPlayer mediaPlayer = new MediaPlayer();
            drawable = (AnimationDrawable) ((ViewHolderYuYin) holder).ivAnim.getBackground();
            ((ViewHolderYuYin) holder).ivAnim.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!Isstart) {
                        try {
                            mediaPlayer.setDataSource(list.get(position).getFile());
                            mediaPlayer.prepare();
                            mediaPlayer.start();
                            if (drawable!=null){
                                drawable.start();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        Isstart=true;
                    }else {
                        mediaPlayer.stop();
                        if (drawable!=null){
                            drawable.stop();
                        }
                        Isstart=false;
                    }
                }
            });
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    if (drawable!=null){
                        drawable.stop();
                    }
                    Isstart=false;
                    notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if(list!=null){
        return list.size();
        }else{
            return 0;
        }
    }

    @Override
    public int getItemViewType(int position) {
        MyMessageBean myMessageBean = list.get(position);
        if(myMessageBean.getType()==1){
            return 1;
        }else if (myMessageBean.getType()==2){
            return 2;
        }else if (myMessageBean.getType()==3){
            return 3;
        }else if (myMessageBean.getType()==4){
            return 4;
        }
        return position;
    }

    //客户端发送文字
    class ViewHodelClient extends RecyclerView.ViewHolder {

        TextView client_context;

        SimpleDraweeView client_simpl;

        public ViewHodelClient(@NonNull View itemView) {
            super(itemView);
            client_context = itemView.findViewById(R.id.client_context);
            client_simpl = itemView.findViewById(R.id.client_simpl);
        }
    }

    //医生端发送文字
    class ViewHodelPatientEnd extends RecyclerView.ViewHolder {

        TextView patients_context;

        SimpleDraweeView patients_simpl;

        public ViewHodelPatientEnd(@NonNull View itemView) {
            super(itemView);
            patients_context = itemView.findViewById(R.id.patients_context);
            patients_simpl = itemView.findViewById(R.id.patients_simpl);
        }
    }
    //患者端发送图片
    class ViewHolderBitMap extends RecyclerView.ViewHolder{
        ImageView img_0001;

        SimpleDraweeView sdv_user0001;
        public ViewHolderBitMap(@NonNull View itemView) {
            super(itemView);
            img_0001 = itemView.findViewById(R.id.img_0001);
            sdv_user0001 = itemView.findViewById(R.id.sdv_user0001);
        }
    }


    //患者端发送语音
    class ViewHolderYuYin extends RecyclerView.ViewHolder {

        private final SimpleDraweeView sdv_user0002;
        private final ImageView ivAnim;
        private final TextView tv_time;

        public ViewHolderYuYin(@NonNull View itemView) {
            super(itemView);
            sdv_user0002 = itemView.findViewById(R.id.sdv_user0002);
            ivAnim = itemView.findViewById(R.id.ivAnim);
            tv_time = itemView.findViewById(R.id.tv_time);
        }
    }
}
